#include "wiki_info_table.h"
#include "sqlite_manager.h"
#include "easylogging++.h"
#include <QDateTime>

WikiInfoTable::WikiInfoTable()
{

}

bool WikiInfoTable::insertNewWiki(QString uuid, QString wikiName, QString parentUuid)
{
    SqliteManager sm;
    QString date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
    bool ret = sm.insertRowData("wiki_info",
    {
        "uuid", "wiki_name", "parent_wiki_uuid", "create_time", "last_modify_time"
    },
    {
        uuid, wikiName, parentUuid, date_time, date_time
    });
    if (!ret)
    {
        LOG(ERROR) << "insert row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiInfoTable::insertWikiInTransiction(QString uuid,
                                            QString wikiName,
                                            QString parentUuid,
                                            SqliteManager &sm)
{
    QString date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz");
    bool ret = sm.insertRowDataInTransaction("wiki_info",
    {
        "uuid", "wiki_name", "parent_wiki_uuid", "create_time", "last_modify_time"
    },
    {
        uuid, wikiName, parentUuid, date_time, date_time
    });

    if (!ret)
    {
        LOG(ERROR) << "insert row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiInfoTable::updateWikiName(QString uuid, QString wikiName)
{
    SqliteManager sm;
    bool ret = sm.updateData("wiki_info",
                             {
                                 {"wiki_name", wikiName}
                             },
                             {
                                 {"uuid", uuid}
                             });
    if (!ret)
    {
        LOG(ERROR) << "update wiki name failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiInfoTable::updateWikiParentUuid(QString uuid, QString parentUuid)
{
    SqliteManager sm;
    bool ret = sm.updateData("wiki_info",
                             {
                                 {"parent_wiki_uuid", parentUuid}
                             },
                             {
                                 {"uuid", uuid}
                             });
    if (!ret)
    {
        LOG(ERROR) << "update wiki parent uuid failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}

bool WikiInfoTable::selectAll(QList<QString> fields, QList<QVariantList> &data)
{
    SqliteManager sm;

    bool ret = sm.selectData("wiki_info", fields, data);
    if (!ret)
    {
        LOG(ERROR) << "select wiki info failed, error info: "
                   << sm.lastError().toStdString();
        return false;
    }

    return true;
}

bool WikiInfoTable::deleteWikiByUuidInTransaction(QString uuid, SqliteManager &sm)
{
    bool ret = sm.deleteData("wiki_info",
    {
        {"uuid", uuid}
    });

    if (!ret)
    {
        LOG(ERROR) << "delete row data failed, error info: "
                   << sm.lastError().toStdString();
    }

    return ret;
}
